#include "Score.h"
#include "ResourceManager.h"
#include <d3dx9.h>

Score::Score(float x, float y)
{
	_sprite = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.Score),0, 0);
	_x = x;
	_y = y;
	_xPre =_x;
	_yPre =_y;
	_width = _sprite->getTexture()->getWidth();
	_height = _sprite->getTexture()->getHeight();
	_score = 0;

	_vx = 0;
	_vy = 1.5;
	_accelY = SCORE_GRAVITY;

	_state = IS_MOVING;
}


Score::~Score(void)
{
	if (_sprite)
	{
		delete _sprite;
	}
	
}

void Score::Update(list<Object *> *_list, float time)
{
	_vy -= _accelY * time;
	_y += _vy * time + 1/2 * _accelY * time * time;
	if (_vy <= 0)
	{
		changeState(IS_IDLE);
	}
	_sprite->Update(time);
}


void Score:: Render(int vpX, int vpY)
{
	
	DrawNumber(_sprite, _score, _x, _y, vpX, vpY);

}

OBJECT_TYPE Score::getObjectType()
{
	return OBJECT_LAND;
}

void Score::DrawNumber(Sprite* sprite, int number, int x, int y, int vpX, int vpY)
{
	RECT rSrc;
	int numWidth=_sprite->getTexture()->getWidth();///10;
	do
	{
		int temp=number%10;
		number=number/10;
		sprite->SetIndex(temp);//_Index = temp;
		sprite->Render(x, y, vpX, vpY);
		x = x - numWidth;
	}while(number>0);
}

void Score::Update(int Score)
{
	 
	this->_score = Score;
}

void Score::changeState(int state)
{
	_state = state;
	switch(_state)
	{	
	case IS_MOVING:
		_vy = 2.5;
		_accelY = SCORE_GRAVITY;
		break;

	case IS_IDLE:
		break;
	}
}